﻿using System;
using System.Collections.Generic;
using UI.Process;
using UI.Process.ServiceReference;

namespace ConsoleApplication
{
    class Program
    {
        private const string ID_2 = "00CE6BFCB7C6EA9683A0CD4EC362E174";
        private const string CLIENT_TAG = "ABC123";
        private static string Token { get; set; }

        static void Main(string[] args)
        {
            try
            {
                var tokenProcessSync = new TokenProcess(CLIENT_TAG);
                Token = tokenProcessSync.GetToken();
                Console.WriteLine("TokenSync: " + Token);

                var tokenProcessAsyncServer = new TokenProcess(CLIENT_TAG);
                tokenProcessAsyncServer.GetTokenAsyncCompleted += GetTokenAsyncServerCompleted;
                tokenProcessAsyncServer.GetTokenServerAsync();

                var tokenProcessAsyncClient = new TokenProcess(CLIENT_TAG);
                tokenProcessAsyncClient.GetTokenAsyncCompleted += GetTokenAsyncClientCompleted;
                tokenProcessAsyncClient.GetTokenClientAsync();

                var allPersonProcessSync = new PersonProcess(CLIENT_TAG, Token);
                WritePersons("PersonSync: ", allPersonProcessSync.GetAll());

                var allPersonProcessServerAsync = new PersonProcess(CLIENT_TAG, Token);
                allPersonProcessServerAsync.GetPersonAsyncCompleted += GetPersonServerAsyncCompleted;
                allPersonProcessServerAsync.GetAllServerAsync();

                var allPersonProcessClientAsync = new PersonProcess(CLIENT_TAG, Token);
                allPersonProcessClientAsync.GetPersonAsyncCompleted += GetPersonClientAsyncCompleted;
                allPersonProcessClientAsync.GetAllClientAsync();

                var idPersonProcessSync = new PersonProcess(CLIENT_TAG, Token);
                WritePersons("PersonSync: ", idPersonProcessSync.GetById(ID_2));

                var idPersonProcessServerAsync = new PersonProcess(CLIENT_TAG, Token);
                idPersonProcessServerAsync.GetPersonAsyncCompleted += GetPersonServerAsyncCompleted;
                idPersonProcessServerAsync.GetByIdServerAsync(ID_2);

                var idPersonProcessClientAsync = new PersonProcess(CLIENT_TAG, Token);
                idPersonProcessClientAsync.GetPersonAsyncCompleted += GetPersonClientAsyncCompleted;
                idPersonProcessClientAsync.GetByIdClientAsync(ID_2);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        static void GetPersonClientAsyncCompleted(object sender, PersonDto[] persons)
        {
            WritePersons("ClientAsync: ", persons);
        }

        static void GetPersonServerAsyncCompleted(object sender, PersonDto[] persons)
        {
            WritePersons("ServerAsync: ", persons);
        }

        private static void WritePersons(string s, IEnumerable<PersonDto> persons)
        {
            Console.WriteLine(s);
            foreach (var person in persons)
            {
                Console.WriteLine(person.Id + ": " + person.Name);
                if (person.Address != null)
                {
                    Console.WriteLine(person.Address.Id + ": " + person.Address.City);
                }
            }
        }

        static void GetTokenAsyncClientCompleted(object sender, string token)
        {
            Token = token;
            Console.WriteLine("AsyncClient: " + Token);
        }

        static void GetTokenAsyncServerCompleted(object sender, string token)
        {
            Token = token;
            Console.WriteLine("AsyncServer: " + Token);
        }
    }
}